package com.simtop.service.impl;

import com.simtop.dao.ExperimentMapper;
import com.simtop.pojo.experiment.Experiment;
import com.simtop.pojo.experiment.ExperimentVo;
import com.simtop.service.ExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lvyongwei
 */
@Service
public class ExperimentServiceImpl implements ExperimentService {

    @Autowired
    private ExperimentMapper experimentMapper;

    /**
     * 添加实验报告
     * @param experiment
     * @return
     */
    @Override
    public boolean addExperiment(Experiment experiment) {
        return experimentMapper.addExperiment(experiment);
    }

    /**
     * 修改内容
     * @param experiment
     * @return
     */
    @Override
    public boolean updateExperiment(Experiment experiment) {
        return experimentMapper.updateExperiment(experiment);
    }

    /**
     * 查询列表
     * @param experimentvo
     * @return
     */
    @Override
    public List<ExperimentVo> getAllExperiment(ExperimentVo experimentvo) {
        return experimentMapper.getAllExperiment(experimentvo);
    }

    /**
     * 获取实验总次数及总用时
     * @return
     */
    @Override
    public Map<String, Long> getChart() {
        List<ExperimentVo> allExperiment = experimentMapper.getAllExperiment(null);
        Map<String, Long> map = new HashMap<String, Long>();
        map.put("totalCount", (long) allExperiment.size());
        long time = 0;
        for (ExperimentVo experimentVo : allExperiment) {
            if(experimentVo.getUseTime() == null){
                time +=0;
            } else{
                time += getSecond(experimentVo.getUseTime());
            }
        }
        map.put("totalTime",time);
        return map;
    }

    /**
     * 获取实验成绩分布
     * @return
     */
    @Override
    public Map<String, Integer> getScoreDistribution() {
        return experimentMapper.getScoreDistribution();
    }

    /**
     * 删除实验报告
     * @param ids
     * @return
     */
    @Override
    public boolean deleteExperiment(Long[] ids) {
        return experimentMapper.deleteExperiment(ids);
    }


    public static long getSecond(String time){
        long s = 0;
        if(time.length()==8){
            int index1=time.indexOf(":");
            int index2=time.indexOf(":",index1+1);
            s = Integer.parseInt(time.substring(0,index1))*3600;
            s+=Integer.parseInt(time.substring(index1+1,index2))*60;
            s+=Integer.parseInt(time.substring(index2+1));
        }
        if(time.length()==5){
            s = Integer.parseInt(time.substring(time.length()-2));
            s+=Integer.parseInt(time.substring(0,2))*60;
        }
        return s;
    }
}
